<?php
namespace Admin\Controller;
use Think\PageAdmin;
use Common\Support\Curl;
use Think\Upload;

/**
 * 师傅管理控制器
 */
class WorkerController extends BaseController
{

    /**
     * 初始化
     * 
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();       
    }

   	/**
   	 * 师傅列表
   	 * @return [type] [description]
   	 */
    public function index(){
        $prefix = C('DB_PREFIX');

        $count = M('worker')->count();
        $page  = new PageAdmin($count, C('PAGE_NUM'));
        
        $pages['show']  = $page->show();  //分页输出
        $pages['total'] = $count;  //总数
        $pages['lists'] = M('worker a')
            ->order("a.id desc")
            ->limit($page->firstRow.','.$page->listRows)
            ->select();

        $this->seotitle = '师傅列表';
        $this->pages    = $pages;
        $this->display();
    }

    /**
     * 师傅详情
     */
    public function detail(){
        $id = I('get.id', '');
        //$id = 12;
        $info = M('worker w')
                ->join("wk_partner p on w.partner_id=p.id")
                ->field('w.*,p.name partner_name')
                ->where(['w.id' => $id])->find();
        

        if (!$info) {
            $this->error('id有误！');
        }

        $count = M('order')->count();
        $page  = new PageAdmin($count, C('PAGE_NUM'));
        
        $pages['show']  = $page->show();  //分页输出
        $pages['total'] = $count;  //总数
        $pages['lists'] = M('order a')
            ->order("a.id desc")
            ->where("a.worker_id=".$id)
            ->limit($page->firstRow.','.$page->listRows)
            ->select();

        $this->pages    = $pages;

        $this->assign('info', $info);
        $this->assign('order', $order);
        $this->seotitle = '师傅详情';
        $this->display();
    }

    /**
     * 审核师傅提交
     */
    public function audit_commit(){
        $id = I('id');
        $status = I('status');
        $audit_info = I('audit_info');

        if(!$id || !$status || !$audit_info){
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '参数异常']);
        }

        $data = [
                    'status'=>$status,
                    'audit_info'=>$audit_info,
                    'audit_time'=>time()
                ];

        $r = M("worker")->where(['id'=>$id])->save($data);

        if($r){
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '审核成功']);
        }else{
            $this->ajaxReturn(['status' => false, 'code' => 1000, 'msg' => '系统错误']);
        }
    }
    

}